package com.itheima.service.impl;

import com.itheima.anno.MyBj;
import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.DeptLog;
import com.itheima.service.DeptLogService;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import net.sf.jsqlparser.parser.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Primary
public class DeptServiceImpl2 implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmpService  empService;

    @Autowired
    private DeptLogService deptLogService;

    @MyBj
    @Override
    public List<Dept> selectAllDept() {
        return deptMapper.selectAllDept();
    }


    @MyBj
    @Transactional(rollbackFor = ParseException.class)
    @Override
    public int delById(Integer deptId) {
        int i = deptMapper.delById(deptId);
        int i2 = empService.delByDeptId(deptId);
        deptLogService.insert(new DeptLog(null,LocalDateTime.now(),i+","+i2));
        return i;
    }

    @Override
    public int insertDept(Dept dept) {

        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        return deptMapper.insertDept(dept);
    }
}
